 /*
 * @name: 管理员工
 * @Author: along
 * @Date: 2019-07-14
 * @Last Modified by: along
 * @Last Modified time: 2020-07-14
 */
<template>
    <div class="container companySystemStaff">
        <div class="companySystemStaff-head">
            <p class="companySystemStaff-head-title">
                管理员工
            </p>
        </div>
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">员工岗位:</p>
                    <el-select
                        v-model="head.pos"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in posList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        :style="{marginRight: '10px', width: '80px', height: '32px'}"
                        @click="dialog.dialogUser = true"
                    >
                        添加员工
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            border
            ref="companySystemStaff"
            class="tableData"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="left"
            />
            <el-table-column
                prop="true_name"
                label="员工姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="phone"
                label="员工手机号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="position"
                label="员工岗位"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.position == 1">
                        精英拓展岗
                    </p>
                    <p v-if="scope.row.position == 2">
                        精英运营岗
                    </p>
                    <p v-if="scope.row.position == 3">
                        私域运营岗-活动
                    </p>
                    <p v-if="scope.row.position == 4">
                        私域运营岗-交易
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                prop="employee_rebate_money"
                label="总提成"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="create_time"
                label="添加时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnRemoveData(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <!-- <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div> -->

        <!-- 选择员工 -->
        <el-dialog
            title="选择员工用户"
            :visible.sync="dialog.dialogVisibleBind"
            width="580px"
            :close-on-click-modal="false"
            class="staff"
        >
            <el-form
                :model="ruleFormBind"
                label-width="100px"
                class="demo-ruleForm"
                @submit.native.prevent
            >
                <el-form-item
                    label="员工用户"
                    class="user-title"
                >
                    <el-input
                        v-model="ruleFormBind.phone"
                        size="small"
                        @keyup.native.enter="fnBindSearch()"
                        placeholder="手机号或用户ID"
                        :style="{'width': '300px'}"
                    />
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnBindSearch()"
                        :style="{width: '76px', height: '32px', position: 'relative', zIndex: '99', left: '-10px'}"
                    >
                        搜索
                    </el-button>
                </el-form-item>
                <el-table
                    :data="tableDataBind"
                    height="100%"
                    style="width: 100%;max-height:500px"
                    border
                    v-if="tableDataBind.length > 0"
                >
                    <el-table-column
                        label="用户姓名"
                        prop="true_name"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="电话号码"
                        prop="phone"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="学校名称"
                        prop="school_name"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="操作"
                        prop="school_name"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <span
                                @click="fnDeleteData(scope.$index)"
                                class="el-delData"
                                style="color: #1890FF;cursor: pointer"
                            >
                                删除
                            </span>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="dialog.dialogVisibleBind = false"
                    size="small"
                    :style="{width: '126px', height: '36px'}"
                >
                    返 回
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    :style="{width: '126px', height: '36px', marginLeft: '52px'}"
                    @click="fnConirmBind()"
                >
                    选 择
                </el-button>
            </span>
        </el-dialog>

        <!-- 添加员工 -->
        <el-dialog
            title="添加员工"
            :visible.sync="dialog.dialogUser"
            width="640px"
            :close-on-click-modal="false"
            class="staff zd-dialogUser"
        >
            <div class="user">
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    ref="ruleForm"
                    label-width="100px"
                    class="demo-ruleForm"
                    :label-position="'right'"
                >
                    <el-form-item
                        label="选择员工"
                        prop="true_name"
                    >
                        <div @click="fnChangeUser()">
                            <div
                                class="user-wrap"
                                v-if="!ruleForm.true_name"
                            >
                                <i class="iconfont el-icon-hand-iconjia"/>
                                选择员工
                            </div>
                            <div
                                v-if="ruleForm.true_name"
                                class="user-info"
                            >
                                {{ ruleForm.true_name + '-' + ruleForm.phone }}
                            </div>
                        </div>
                    </el-form-item>
                    <el-form-item
                        label="选择岗位"
                        prop="elite_development"
                    >
                        <el-select
                            v-model="ruleForm.elite_development"
                            clearable
                            filterable
                            placeholder="请选择"
                            size="small"
                            :style="{width: '452px', height: '32px'}"
                        >
                            <el-option
                                v-for="item in posListDialog"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item
                        label="员工姓名"
                        prop="elite_name"
                    >
                        <el-input
                            placeholder="请输入员工真实姓名"
                            v-model="ruleForm.elite_name"
                            size="small"
                            clearable
                            :style="{width: '452px', height: '32px'}"
                        />
                    </el-form-item>
                </el-form>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
                style="margin-top: 10px"
            >
                <el-button
                    @click="()=>{
                        this.$router.push({
                            name: 'companyStaffSeting'
                        });
                    }"
                    size="small"
                    :style="{width: '126px', height: '36px'}"
                >
                    返回列表
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    :style="{width: '126px', height: '36px', marginLeft: '52px'}"
                    @click="fnSubmitData('ruleForm')"
                >
                    提 交
                </el-button>
            </span>
        </el-dialog>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            ruleForm: {
                true_name: '',
                phone: '',
                student_no: '',
                elite_development: '',
                elite_name: ''
            },
            rules: {
                true_name: [
                    { required: true, message: '请选择精英', trigger: 'change' }
                ],
                elite_development: [
                    { required: true, message: '请选择岗位', trigger: 'change' }
                ],
                elite_name: [
                    { required: true, message: '请输入员工姓名', trigger: 'change' }
                ]
            },
            tableDataBind: [],
            ruleFormBind: {
                phone: ''
            },
            dialog: {
                dialogVisibleBind: false,
                dialogUser: false
            },
            head: {
                pos: 99
            },
            posList: [{
                value: 99,
                label: '全部'
            },{
                value: 1,
                label: '精英拓展岗'
            },{
                value: 2,
                label: '精英运营岗'
            },{
                value: 3,
                label: '私域运营岗-活动'
            },{
                value: 4,
                label: '私域运营岗-交易'
            }],
            posListDialog: [{
                value: 1,
                label: '精英拓展岗'
            },{
                value: 2,
                label: '精英运营岗'
            },{
                value: 3,
                label: '私域运营岗-活动'
            },{
                value: 4,
                label: '私域运营岗-交易'
            }],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {
        dialog: {
            handler: function (val, oldVal) {
                if(!val.dialogUser) {
                    //
                }
            },
            deep: true,
        }
    },
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '掌大精英'},{title: '管理员工'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/stuagent_employee%5Cget_list', {
                position: this.head.pos
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    // this.page.now_page = resp.data.page.now_page;
                    // this.page.total_count = resp.data.page.total_count;
                    // this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 删除
         */
        fnRemoveData (row) {
            this.$confirm('是否删除该员工并解绑其与精英的分佣激励？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/stuagent_employee%5Cdelete',{
                    student_no: row.student_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '警告',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * 搜索
         */
        fnBindSearch () {
            if(this.ruleFormBind.phone == '') {
                this.$notify({
                    title: '警告',
                    message: '请输入手机号或用户ID',
                    type: 'warning',
                    duration: 2000
                });
                return;
            }

            this.$post('/student/stuagent%5Cget_student_simple_list',{
                keyword_student_id: this.ruleFormBind.phone
            },resp=>{
                if (resp.code == 1) {
                    if (resp.data.list.length == 0) {
                        this.$notify({
                            title: '警告',
                            message: '未搜索到该用户',
                            type: 'warning',
                            duration: 2000
                        });
                        this.ruleFormBind.phone = '';
                        return;
                    }
                    this.tableDataBind = resp.data.list;
                    this.ruleFormBind.phone = '';
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '警告',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * 删除搜索后的员工
         */
        fnDeleteData (index) {
            this.tableDataBind.splice(index,1);
        },

        /**
         * 确定添加员工
         */
        fnConirmBind () {
            if (this.tableDataBind.length == 0) {
                this.$notify({
                    title: '警告',
                    message: '请输入手机号或用户ID',
                    type: 'warning',
                    duration: 2000
                });
                return;
            }

            if(this.tableDataBind.length == 1) {
                this.$set(this.ruleForm, 'true_name', this.tableDataBind[0].true_name);
                this.$set(this.ruleForm, 'phone', this.tableDataBind[0].phone);
                this.$set(this.ruleForm, 'student_no', this.tableDataBind[0].student_no);
                this.dialog.dialogVisibleBind = !this.dialog.dialogVisibleBind;
            } else if(this.tableDataBind.length > 1){
                this.$notify({
                    title: '警告',
                    message: '最多只可选择一个',
                    type: 'warning',
                    duration: 2000
                });
            } else {
                this.$notify({
                    title: '警告',
                    message: '用户不可以为空',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * 选择员工
         */
        fnChangeUser () {
            this.dialog.dialogVisibleBind = !this.dialog.dialogVisibleBind;
        },

        /**
         * 确定添加员工
         */
        fnSubmitData (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$post('/student/stuagent_employee%5Cadd',{
                        student_no: this.ruleForm.student_no,
                        position: this.ruleForm.elite_development,
                        true_name: this.ruleForm.elite_name
                    },resp=>{
                        if (resp.code == 1) {
                            this.$notify({
                                title: '提示',
                                message: '添加成功',
                                type: 'success',
                                duration: 2000
                            });
                            this.getTableData();
                            this.dialog.dialogUser = !this.dialog.dialogUser;
                        } else if (resp.code == 0) {
                            this.$notify({
                                title: '警告',
                                message: resp.msg,
                                type: 'warning',
                                duration: 2000
                            });
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 查看图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        padding-bottom: 15px;
        .tableData {
            width: calc(100% - 48px);
            margin: 0 24px 0 24px;
        }
        .companySystemStaff-head {
            width: 100%;
            height: 68px;
            display: flex;
            align-items: center;
            box-sizing: border-box;
            padding: 0 35px;
            border-bottom: 1px #f4f4f4 solid;
            margin-bottom: 24px;
            justify-content: space-between;
            &-title {
                color: #333333;
                font-size: 20px;
                font-weight: 500;
                position: relative;
                &::before {
                    content: '';
                    position: absolute;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                    top: 0px;
                    margin-left: -10px;
                }
            }
            &-wrap {
                display: flex;
                align-items: center;
                height: 32px;
            }
        }
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            padding-left: 24px;
            margin-bottom: 14px;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding: 17px 24px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .staff {
        &-title {
           color: #333333;
           font-size: 14px;
           margin-bottom: 24px;
           display: inline-block;
        }
        .dialog-footer {
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        &-wrap {
            width: 100%;
            display: flex;
            flex-wrap: wrap;
            box-sizing: border-box;
            .staff-wrap-name {
                width: 100%;
                display: flex;
                justify-content: center;
                margin-top: 20px;
                margin-bottom: 22px;
                color: #333;
                font-size: 14px;
            }
            .staff-radio {
                width: 115px;
                height: 15px;
                display: flex;
                align-items: center;
                margin-bottom: 24px;
            }
        }
    }
    .user {
        .user-wrap {
            width:452px;
            height:32px;
            border-radius:4px;
            border:1px dashed rgba(184,184,184,1);
            display: flex;
            justify-content: center;
            align-items: center;
            color: #20A0FF;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            .el-icon-hand-iconjia {
                font-size: 15px;
                font-weight: bold;
                margin-right: 10px;
                color: #20A0FF;
            }
        }
        .user-info {
            width:452px;
            height:32px;
            background:rgba(255,255,255,1);
            border-radius:4px;
            border:1px solid rgba(0,0,0,0.15);
            display: flex;
            align-items: center;
            padding-left: 12px;
            color: #333333;
            font-size: 14px;
            cursor: pointer;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    }
    .upload {
        width: 100%;
        display: flex;
        justify-content: center;
        position: relative;
        .upload-wrap {
            display: flex;
            justify-content: center;
            align-items: center;
            width:520px;
            height:36px;
            border-radius:4px;
            border:1px dashed rgba(194,194,194,1);
            color: #C2C2C2;
            font-size: 14px;
            cursor: pointer;
            .el-icon-upload2 {
                color: #C2C2C2;
                font-weight: bold;
                margin-right: 10px;
            }
        }
        .upload-wrap-success {
            display: flex;
            justify-content: space-between;
            padding: 0 12px;
            width:520px;
            height:36px;
            border-radius:4px;
            border:1px dashed rgba(194,194,194,1);
            color: #333333;
            font-size: 14px;
            align-items: center;
            cursor: pointer;
            .el-icon-upload2 {
                color: #C2C2C2;
                font-weight: bold;
                margin-right: 10px;
            }
        }
        .import-input {
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            width: 520px;
            height: 36px;
            cursor: pointer;
        }
    }
    .upload-main {
        width: 520px;
        height: 15px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 12px;
        margin-bottom: 30px;
        &-text {
            color: #999999;
            font-size: 14px;
        }
        .upload-down {
            color: #1890FF;
            font-size: 14px;
            cursor: pointer;
        }
    }
</style>
<style lang="less">
    .companySystemStaff{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-dialog__header {
            padding-top: 10px;
            box-shadow:0px 1px 0px 0px rgba(233,233,233,1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }
            .el-icon-close:before {
                color: rgb(194,194,194);
                font-size: 17px;
                font-weight: bold;
            }
            .el-dialog__headerbtn {
                top: 14px;
            }
        }
        .el-radio-group {
            display: flex;
            flex-wrap: wrap;
        }
        .el-dialog__body {
            padding-bottom: 0px;
        }
        .user-title .el-form-item__label {
            color: #333;
            font-size: 14px;
        }
        .user{
            .el-form-item__label {
                line-height: 32px;
                color: #333;
                margin-top: 4px;
            }
        }
        .zd-dialogUser {
            .el-dialog__body {
                padding-left: 27px;
            }
        }
        .el-dialog {
            border-radius: 4px;
        }
        .el-dialog__header {
            border-radius: 4px 4px 0 0;
        }
    }
</style>